package com.cls.common.utils;

import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.cls.generator.entity.GeneratorConfig;
import com.cls.generator.entity.GeneratorConstant;
import com.cls.generator.helper.GeneratorHelper;
import com.google.common.io.Files;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateExceptionHandler;
import org.apache.commons.io.FileUtils;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Objects;
import java.util.regex.Matcher;

/**
 * 导出工具类
 *
 * @author Administrator
 */
public class ReportUtil {
    private static final String TEMPLATE_PATH = "/templates/export/";

    /**
     * 填充信息
     */
    public static void generateFileByTemplate(String templateName, String fileName, Object data) throws Exception {
        String tempPath = fileName;
        File file = new File(tempPath);
        Template template = getTemplate(templateName);
        Files.createParentDirs(file);
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        try (Writer out = new BufferedWriter(new OutputStreamWriter(fileOutputStream, StandardCharsets.UTF_8), 10240)) {
            template.process(data, out);
        } catch (Exception e) {
            String message = "代码生成异常";
            throw new Exception(message);
        }
    }

    /**
     * 获取模板
     *
     * @param templateName
     * @return
     * @throws Exception
     */
    private static Template getTemplate(String templateName) throws Exception {
        Configuration configuration = new freemarker.template.Configuration(Configuration.VERSION_2_3_23);
        String templatePath = GeneratorHelper.class.getResource(TEMPLATE_PATH).getPath();
        File file = new File(templatePath);
        if (!file.exists()) {
            templatePath = System.getProperties().getProperty("java.io.tmpdir");
            file = new File(templatePath + File.separator + templateName);
            InputStream resourceAsStream = GeneratorHelper.class.getResourceAsStream(TEMPLATE_PATH + templateName);
            FileUtils.copyInputStreamToFile(Objects.requireNonNull(resourceAsStream), file);
        }
        configuration.setDirectoryForTemplateLoading(new File(templatePath));
        configuration.setDefaultEncoding("UTF-8");
        configuration.setTemplateExceptionHandler(TemplateExceptionHandler.IGNORE_HANDLER);
        return configuration.getTemplate(templateName);
    }

    /**
     * 获取/templates/export/目录下文件
     *
     * @param fileName
     * @return
     * @throws Exception
     */
    public static File getFile(String fileName) throws IOException {
        Configuration configuration = new freemarker.template.Configuration(Configuration.VERSION_2_3_23);
        String templatePath = GeneratorHelper.class.getResource(TEMPLATE_PATH).getPath();
        File file = new File(templatePath);
        if (!file.exists()) {
            templatePath = System.getProperties().getProperty("java.io.tmpdir");
            file = new File(templatePath + File.separator + fileName);
            InputStream resourceAsStream = GeneratorHelper.class.getResourceAsStream(TEMPLATE_PATH + fileName);
            FileUtils.copyInputStreamToFile(Objects.requireNonNull(resourceAsStream), file);
        }
        return file;
    }

}
